package com.example.njut;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;

public class RecordActivity extends AppCompatActivity {
    private ImageView stopView;
    private Button recordStart;
    private Button recordStop;
    private Button playStart;
    private Button playStop;
    private WebView runView;

    MediaRecorder mRecorder = null;
    MediaPlayer mPlayer = null;
    String FileName = null;


    //声明数组，用来保存所有需要动态开启的权限
    private static String[] PERMISSIONS_STORGE = {
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS
    };
    //权限的请求编码
    private static int REQUEST_PERMISSION_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
            if(ActivityCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO)!=
                    PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,PERMISSIONS_STORGE,REQUEST_PERMISSION_CODE);
            }
            if(ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=
                    PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,PERMISSIONS_STORGE,REQUEST_PERMISSION_CODE);
            }
            if(ActivityCompat.checkSelfPermission(this,Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS)!=
                    PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,PERMISSIONS_STORGE,REQUEST_PERMISSION_CODE);
            }
        }

        stopView = (ImageView) findViewById(R.id.stopView);
        recordStart = (Button) findViewById(R.id.record_start);
        recordStop = (Button) findViewById(R.id.record_stop);
        playStart = (Button) findViewById(R.id.play_start);
        playStop = (Button) findViewById(R.id.play_stop);
        runView = (WebView)findViewById(R.id.runView);
        runView.loadUrl("file:///android_asset/play.gif");
        FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        FileName+="/myRecord.amr";

        recordStop.setEnabled(false);
        playStop.setEnabled(false);
        playStart.setEnabled(false);
        recordStart.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View view) {
                stopView.setVisibility(View.INVISIBLE);
                runView.setVisibility(View.VISIBLE);
                recordStart.setEnabled(false);
                recordStop.setEnabled(true);
                playStart.setEnabled(false);

                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mRecorder.setOutputFile(FileName);
                try{
                    mRecorder.prepare();
                    mRecorder.start();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });

        recordStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopView.setVisibility(View.VISIBLE);
                runView.setVisibility(View.INVISIBLE);
                recordStart.setEnabled(true);
                recordStop.setEnabled(false);
                playStart.setEnabled(true);
                mRecorder.stop();
                mRecorder.release();
                mRecorder = null;
            }
        });

        playStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordStart.setEnabled(false);
                playStop.setEnabled(true);
                playStart.setEnabled(false);
                mPlayer = new MediaPlayer();
                try{
                    mPlayer.setDataSource(FileName);
                    mPlayer.prepare();
                    mPlayer.start();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });

        playStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordStart.setEnabled(true);
                playStop.setEnabled(false);
                playStart.setEnabled(true);
                try {
                    mPlayer.stop();
                    mPlayer.release();
                    mPlayer = null;
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
}
